import { defineStore } from 'pinia'
import type { TeachingResource, TeachingResourceComment } from '../../types/Resource'

/**
 * 教学资源存储模块
 * 负责管理教学资源的全局状态
 */
export const useTeachingResourceStore = defineStore('teachingResource', {

    // 持久化配置
    persist: {
        storage: localStorage,
        paths: ['resources', 'currentResource', 'searchKeyword']
    },
    state: () => ({
        // 所有资源列表
        resources: [] as TeachingResource[],
        // 当前查看的资源
        currentResource: null as TeachingResource | null,
        // 当前资源的评论
        comments: [] as TeachingResourceComment[],
        // 加载状态
        loading: false,
        // 资源类型筛选
        typeFilter: null as number | null,
        // 搜索关键词
        searchKeyword: ''
    }),
    actions: {
        /**
         * 获取所有资源
         */
        async fetchAllResources() {
            this.loading = true
            try {
                // this.resources = await resourceApi.getAll()
            } finally {
                this.loading = false
            }
        },

        /**
         * 获取资源详情
         * @param resourceId - 资源ID
         */
        async fetchResourceDetail(resource) {
            this.loading = true
            try {
                this.currentResource = await resource
                // this.comments = await resourceApi.getComments(resourceId)
            } finally {
                this.loading = false
            }
        },

        /**
         * 上传新资源
         * @param newResource - 新资源对象
         */
        async uploadResource(newResource: Omit<TeachingResource, 'resourceId' | 'uploadTime'>) {
            this.loading = true
            try {
                // const created = await resourceApi.upload(newResource)
                // this.resources.unshift(created)
            } finally {
                this.loading = false
            }
        },

        /**
         * 增加下载次数
         * @param resourceId - 资源ID
         */
        async incrementDownload(resourceId: number) {
            // await resourceApi.incrementDownload(resourceId)
            const resource = this.resources.find(r => r.resourceId === resourceId)
            if (resource) {
                resource.downloadCount = (resource.downloadCount || 0) + 1
            }
        }
    },
    getters: {
        /**
         * 获取筛选后的资源列表
         */
        filteredResources(state): TeachingResource[] {
            let result = [...state.resources]

            // 类型筛选
            if (state.typeFilter !== null) {
                result = result.filter(r => r.resourceType === state.typeFilter)
            }

            // 关键词搜索
            if (state.searchKeyword.trim()) {
                const keyword = state.searchKeyword.toLowerCase()
                result = result.filter(r =>
                    r.resourceName.toLowerCase().includes(keyword) ||
                    (r.resourceDescription && r.resourceDescription.toLowerCase().includes(keyword)))
            }

            return result
        },

        /**
         * 获取热门资源（按下载量排序）
         */
        popularResources(state): TeachingResource[] {
            return [...state.resources]
                .sort((a, b) => (b.downloadCount || 0) - (a.downloadCount || 0))
                .slice(0, 5)
        }
    }
})
